package cn.oowoo.rabbitmq.confirm;

import cn.oowoo.rabbitmq.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
* @Author: Zhangbin
* @Date: 11:24 2021/5/9
* @Description: confirm模式单条消息逐一确认测试-生产者
**/
public class NormalConfirmProducer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = RabbitConfig.createConnection();
        // 创建消息通道
        Channel channel = connection.createChannel();
        //开启发送方确认模式
        channel.confirmSelect();

        for(int i=0;i<3;i++){
            String msg = "测试confirm消息.... "+i;
            channel.basicPublish("",RabbitConfig.QUEUE_CONFIRM_NAME,null,msg.getBytes());
            /*
                普通Confirm，发送一条，确认一条。waitForConfirms处会造成当前程序的阻塞
             */
            if (channel.waitForConfirms()) {
                System.out.println("消息发送成功!!" );
            }
        }
        //消息发送完毕关闭信道，关闭链接
        channel.close();
        connection.close();
    }
}
